<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.0/css/bootstrap.css" rel="stylesheet">
  <style>
      .ani-enter{
      transform: translateX(-500px) ;
      opacity: 0;
    }
    .ani-leave-to{
      transform: translateX(500px);
      opacity: 0;
    }
    .ani-enter-active,.ani-leave-active{
      transition: all  500ms;
    }
  </style>
</head>

<body>
  <div id="app" style="max-width: 800px;margin: 20px auto">
      <div class="header row">
        <div class="col-md-10 col-sm-10">
          <input v-model="todo" type="text" class="form-control">
        </div>
        <div class="col-md-2 col-sm-2">
          <button class="btn btn-primary" @click="addTodo">增加</button>
        </div>
      </div>
      <div class="list" style="margin-top: 20px">
        
        <ul class="list-group">
          <li class="list-group-item clearfix text-center" v-if="!todos.length">
            暂无代办事项
          </li>
          <transition-group name="ani">
            <li  
              class="list-group-item clearfix" 
              v-for="(todo,index) in todos"
              :key="index"
              >
              {{index}}
              <span>{{todo.content}}</span>
              <div class="btns pull-right">

                <button @click="changeTodoState(index)" :disabled="todo.isCompleted" class="btn btn-success btn-sm">点击完成</button>
                <button @click="delTodo(index)" class="btn btn-danger btn-sm">删除</button>
              </div>
            </li>
        </transition-group>
        </ul>
      </div>
  </div>
  <script src="./vue.js"></script>
  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        todo: '', // 文本框输入值
        todos: [ // todo列表数据
          {
            content:'明天去爬山',
            isCompleted: false
          }
        ]
      },
      methods: {
        addTodo(){
          // 添加待办事项
          if(this.todo.trim() === ''){
            return;
          }
          this.todos.push({
            content: this.todo,
            isCompleted: false
          });
          this.todo = '';
        },
        delTodo(index){
          if(confirm("您确认删除吗?")){
            this.todos.splice(index, 1);
          }
        },
        changeTodoState(index){
          // 点击已完成
          this.todos[index].isCompleted = true;
        }
      }
    })
  </script>
</body>

</html>